<?php


namespace app\api\controller\v1;


use app\api\model\Currency;
use app\api\model\UserAssetsAuth;
use LinCmsTp5\exception\ParameterException;

class UserAssets
{
    /**
     * 资产认证列表
     * @validate('PagingParameter')
     */
    public function getDataList($page,$limit)
    {
        $where = [];
        $query = UserAssetsAuth::where($where)->with('AssetsImg')
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->select()->each(function (&$item){
            if($item['status'] == 1) {
                $item['status_name'] = '审核中';
            } elseif($item['status'] == 2) {
                $item['status_name'] = '审核通过';
            } elseif($item['status'] == 3) {
                $item['status_name'] = '审核不通过';
            }
        })->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    /**
     * 收入认证详情
     * @validate('IdMustBePositiveInt')
     */
    public function getDataInfo($id)
    {
        return UserAssetsAuth::get($id);
    }

    /**
     * 收入认证通过 添加甜茶币
     */
    public function updateData($id,$status,$sta_msg)
    {
        $info = UserAssetsAuth::get($id);
        if($info){
            if($status == 2) {
                UserAssetsAuth::where('id',$id)->update([
                    'status' => 2
                ]);
                $currency = Currency::where('status',2)
                    ->where('openid',$info['openid'])
                    ->where('auth_type',6)
                    ->find();
                if(!$currency) {
                    Currency::create([
                        'openid' => $info['openid'],
                        'auth_type' => 6,
                        'status' => 2,
                        'number' => 25,
                        'min_number' => 25,
                        'end_time' => (time() + 604800),
                        'msg_json' => json_encode($info)
                    ]);
                }
            } else {
                UserAssetsAuth::where('id',$id)->update([
                    'status' => 3,
                    'sta_msg' => $sta_msg,
                ]);
            }
            return writeJson(201, [], '操作成功！');
        } else {
            throw new ParameterException([
                'msg' => [
                    'auth' => '信息查询失败'
                ]
            ]);
        }
    }
}